package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysAccountRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysAccountMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysAccount;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysAccountService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 人信息账户业务层处理
 *
 * @author mars
 * @date 2024-03-16
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysAccountServiceImpl implements ISysAccountService {

    private final SysAccountMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysAccount add(SysAccountRequest request) {
        SysAccount entity = SysAccount.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysAccountRequest request) {
        SysAccount entity = SysAccount.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public SysAccount getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysAccount> pageList(SysAccountRequest request) {
        Page<SysAccount> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysAccount> query = this.buildWrapper(request);
        IPage<SysAccount> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysAccount> list(SysAccountRequest request) {
        LambdaQueryWrapper<SysAccount> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysAccount> buildWrapper(SysAccountRequest param) {
        LambdaQueryWrapper<SysAccount> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getName())){
               query.like(SysAccount::getName ,param.getName());
        }
         if (StringUtils.isNotBlank(param.getAccount())){
               query.like(SysAccount::getAccount ,param.getAccount());
        }
         if (StringUtils.isNotBlank(param.getBalance())){
               query.like(SysAccount::getBalance ,param.getBalance());
        }
         if (StringUtils.isNotBlank(param.getBank())){
               query.like(SysAccount::getBank ,param.getBank());
        }
        return query;
    }

}
